/**************************************************************
 * @file     bsp_led.c
 * @brief    接收队列消息驱动led
 * @author   Ruilist
 * @date     2025-07-30
 * @version  V1.0.0
 * @copyright Copyright (c) 2025
 *
 * @note     1 tab = 4 space
 *
 * @par ??:
 * - V1.0.0 2025-07-30 Ruilist
 *   接收队列消息驱动led
 **************************************************************/

#include "bsp_led.h"

osThreadId_t         led_TaskHandle;
const osThreadAttr_t led_Task_attributes = {
    .name       = "led_Task",
    .stack_size = 128 * 4,
    .priority   = (osPriority_t)osPriorityNormal,
};

QueueHandle_t led_queue;

static uint32_t g_blink_times = 0;  // 每秒闪烁次数
static uint32_t g_blink_order = 0;  // 闪烁的亮灭操作

/**
 * @brief   led任务
 *
 * @param   *argument    默认参数
 *
 * @return  无
 *
 */
void led_task_func( void *argument )
{
    led_status_t    led_ret   = LED_OK;
    led_operation_t led_value = LED_ON;
    led_queue                 = xQueueCreate( 10, sizeof( led_operation_t ) );
    if ( NULL == led_queue )
    {
        printf( "led_queue created failed \r\n" );
    }
    else
    {
        printf( "led_queue created successfully \r\n" );
    }
    for ( ;; )
    {
        printf( "start led_task_func\r\n" );
        if ( led_queue != 0 )
        {
            if ( pdTRUE == xQueueReceive( led_queue, &( led_value ), (TickType_t)0 ) )
            {
                printf( "received led_queue value = [%d] at time [%d] \r\n ", led_value, HAL_GetTick() );
                led_ret = led_on_off_timer_irq( led_value );
                if ( LED_OK == led_ret )
                {
                    printf( "led_on_off successfully at time [%d] \r\n", HAL_GetTick() );
                }
            }
        }
        osDelay( 100 );
    }
}

/**
 * @brief   实现led亮灭等状态
 *
 * @param   led_operation    led操作参数
 *
 * @return  函数状态 led_status_t
 *
 */
led_status_t led_on_off( led_operation_t led_operation )
{
    led_status_t ret = LED_OK;

    if ( LED_ON == led_operation )
    {
        HAL_GPIO_WritePin( LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET );  // led on
    }
    if ( LED_OFF == led_operation )
    {
        HAL_GPIO_WritePin( LED_GPIO_Port, LED_Pin, GPIO_PIN_SET );  // led off
    }
    if ( LED_TOGGLE == led_operation )  // led toggle
    {
        HAL_GPIO_TogglePin( LED_GPIO_Port, LED_Pin );
    }
    if ( LED_BLINK_3_TIMES == led_operation )
    {
        for ( int i = 0; i < 6; i++ )
        {
            HAL_GPIO_TogglePin( LED_GPIO_Port, LED_Pin );
            osDelay( 300 );
        }
    }
    return ret;
}

/**
 * @brief   实现led亮灭状态，用于回调函数中调用
 *
 * @param   led_operation    led操作参数
 *
 * @return  函数状态 led_status_t
 *
 */
led_status_t led_on_off_timer_irq( led_operation_t led_operation )
{
    led_status_t ret = LED_OK;

    if ( LED_BLINK_1_TIMES == led_operation )
    {
        g_blink_times = 1;
        g_blink_order = 0;
    }
    if ( LED_BLINK_10_TIMES == led_operation )
    {
        g_blink_times = 10;
        g_blink_order = 0;
    }

    return ret;
}

/**
 * @brief   TIM2中断回调函数
 *
 * @param   void
 *
 * @return  函数状态 led_status_t
 *
 */
led_status_t led_callback_in_timer( void )
{
    if ( g_blink_times > 0 )
    {
        if ( g_blink_order % 2 == 0 )
        {
            led_on_off( LED_ON );
        }
        else
        {
            led_on_off( LED_OFF );
            g_blink_times--;
        }
        g_blink_order++;
    }
    else
    {
        g_blink_order = 0;
    }

    return LED_OK;
}
